const service = require('./service')
const config = require('../config/config')
const useragent = require('../common/useragent')

/**
 * 构建请求头信息
 */
async function getBaseOptions() {
  var resultCookie = await service.getCookie()
  var us = useragent.getRondom()
  var baseOptions = {
    json: true,
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Accept-Encoding': 'deflate',
      'Accept-Language': 'zh-CN,zh;q=0.9',
      Connection: 'keep-alive',
      Cookie: resultCookie,
      Host: 'index.baidu.com',
      "Referer'": 'http://index.baidu.com/v2/main/index.html',
      'User-Agent': us,
      'X-Requested-With': 'XMLHttpRequest'
    }
  }
  return baseOptions
}

/**
 * 返回JSON数据
 * @param {*} message 返回信息
 * @param {*} isSuccess 是否成功
 * @param {*} result 返回结果
 */
function resultJson(message, isSuccess = false, result = null) {
  var result = { isSuccess: isSuccess, message: message, result }
  return result
}

/**
 * 返回JSONLIST数据 适配layui
 * @param {*} count 总条数
 * @param {*} data 返回结果
 */
function resultListJson(count = 0, data = null) {
  var result = { code: 0, msg: '', count, data }
  return result
}

module.exports = {
  getBaseOptions,
  resultJson,
  resultListJson
}
